package com.etc.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.etc.entity.Foods;
import com.etc.entity.RFoods;
import com.etc.entity.Restaurant;
import com.etc.service.FoodsService;
import com.etc.utils.CommonMessage;

@RequestMapping(value = "adminapi")
@Controller
public class AFoodsController {
	@Resource
	private FoodsService foodsService;

	@RequestMapping(value = "foodslistbyrid/{rid}")
	public String selectAllfoodsbyrid(@PathVariable("rid") Integer rid, Model model) {
		List<RFoods> list = foodsService.getFoodsAllByRid(rid);
		model.addAttribute("list", list);
		return "admin/foods-list";
	}

	@RequestMapping(value = "foodslist")
	public String selectAllfoods(Model model) {
		List<RFoods> list = foodsService.getFoodsAll();
		model.addAttribute("list", list);
		return "admin/foods-list";
	}

	@RequestMapping(value = "toaddfoods")
	public String toaddfoods(Integer rid, Model model) {
		model.addAttribute("rid", rid);
		return "admin/foods-add";
	}

	@RequestMapping(value = "addfoods")
	public String addfoods(Integer rid, String fname, String fcontent, Double fprice,
			@RequestParam(value = "myfile") MultipartFile file, HttpServletRequest request) {
		String str = file.getOriginalFilename();
		String[] arr = str.split("\\.");
		if (arr[arr.length - 1].equals("jpg") || arr[arr.length - 1].equals("png")) {
			// 获取上传路径
			String path = "D:\\imgs\\foods" + "\\" + file.getOriginalFilename();
			// 创建目标文件
			File destFile = new File(path);
			try {
				FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String fimg = "/imgUrl/foods/" + file.getOriginalFilename();
			Foods foods = new Foods(null, fname, fimg, fcontent, fprice, rid);
			boolean flag = foodsService.addfoods(foods);
			if (flag) {
				return "redirect:foodslistbyrid/" + rid;
			} else {
				request.setAttribute("msg", "新增失败");
			}

			request.setAttribute("msg", "新增成功");
			return "redirect:foodslistbyrid/" + rid;
		} else {
			request.setAttribute("msg", "新增失败");
			return "redirect:foodslistbyrid/" + rid;
		}
	}
	
	
	@RequestMapping(value="delfoods/{fid}",method=RequestMethod.DELETE)
	@ResponseBody
	public CommonMessage delrestaurant(@PathVariable(value="fid") int fid) {
		CommonMessage cm = new CommonMessage();
		boolean flag = foodsService.delfoodsByFid(fid);
		cm.setMsg(flag?"删除成功":"删除失败");
		return cm;
	}
	
	@RequestMapping(value="toupdatefoods")
	public String toupdaterestaurant(Foods foods, Model model) {
		model.addAttribute("foods", foods);
		return "admin/foods-update";
	}
	
	@RequestMapping(value="updatefoods")
	@ResponseBody
	public CommonMessage editrestaurant(@RequestBody Foods foods) {
		CommonMessage cm = new CommonMessage();
		boolean flag = foodsService.updatafoods(foods);
		cm.setMsg(flag?"修改成功":"修改失败");
		return cm;
	}
}
